package com.ld.security.common.mybatis.core;

import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.ld.security.common.core.util.ReflectionUtil;
import lombok.extern.log4j.Log4j2;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.expression.EvaluationContext;

import java.lang.reflect.Method;

@Log4j2
public class DsArgsProcessor extends DsProcessor {

    private static final String HEADER_PREFIX = "#";

    @Override
    public boolean matches(String key) {

        return true;
    }

    @Override
    public String doDetermineDatasource(MethodInvocation invocation, String key) {



        key = key.replace("#","");

        Object[] args =  invocation.getArguments();
        String db = key;
        try {
            db =  ReflectionUtil.invokeGetter(args,key);
        }catch (Exception e){
           // e.printStackTrace();
        }
        log.info("使用数据源："+db);
        return db;
    }
}